package com.example.csv2db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class App {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(App.class);
        logger.info("Start the program.");

        // Creating the application context object
        ApplicationContext context = new ClassPathXmlApplicationContext("jobconfig.xml");

        // Creating the job launcher
        JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");

        // Creating the job
        Job job = (Job) context.getBean("helloWorldJob");

        // Executing the JOB
        JobExecution execution;
        try {
            execution = jobLauncher.run(job, new JobParameters());
            logger.info("Exit Status : " + execution.getStatus());
        } catch (JobExecutionAlreadyRunningException e) {
            logger.error(e.getMessage());
        } catch (JobRestartException e) {
            logger.error(e.getMessage());
        } catch (JobInstanceAlreadyCompleteException e) {
            logger.error(e.getMessage());
        } catch (JobParametersInvalidException e) {
            logger.error(e.getMessage());
        }

        logger.info("Finish the program.");
    }

}
